package com.zegome.game.oanquan.scene.menu;

import android.util.Log;
import android.view.KeyEvent;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.oanquan.GameCenter;
import com.zegome.game.oanquan.ImageAssets;
import com.zegome.game.oanquan.OAnQuanController;
import com.zegome.game.oanquan.SoundAsset;
import com.zegome.game.oanquan.scene.OAnQuanScreen;
import com.zegome.game.oanquan.scene.UICenter;
import com.zegome.game.screen.ITranScreenAction;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class MenuScreen extends OAnQuanScreen{

	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================
//	private MenuUI mMenuUI;
	
	IFacebookBridge mFBBridge;
	
	private Music mMenuMusic;
	
	// ===========================================================
	// Constructor
	// ===========================================================
	public MenuScreen(AndroidGameController controller, String name, final IFacebookBridge bridge) {
		super( controller, name);
		mController = (OAnQuanController) mController;
		mFBBridge = bridge;
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public void setFBBridge(final IFacebookBridge bridge) {
		mFBBridge = bridge;
	}
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void onCreate() {
		super.onCreate();
		final TextureAtlas menuAtlas = mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.ATLAS_BGMENU, TextureAtlas.class);
		final TextureRegion bgRegion = menuAtlas.findRegion(ImageAssets.REGION_BG_MENU);
		final Sprite bg = new Sprite(0, 0, bgRegion);
		attachChild(bg);
		
		mMenuMusic = mController.get(SoundAsset.ASSET_MUSIC_FOLDER + SoundAsset.ASSET_MUSIC_MENU, Music.class);
		mMenuMusic.setLooping(true);
		
		mUICenter.start();
	}
	
	@Override
	public boolean onKeyUp(int keyCode) {		
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			final boolean isProcess = mUICenter.back();
			if (!isProcess) {
				Gdx.app.exit();
			}
		}
		return false;
	}

	@Override
	public void nextScreen() {
		transitionScreen(new ITranScreenAction() {

			@Override
			public void onLoadComplete() {
			}

			@Override
			public void onLoad() {
				if (GameCenter.isMusic) {
					mMenuMusic.pause();
				}
				mUICenter.hideAutoBack();
			}

			@Override
			public boolean isNeedUpdateAsset() {
				return false;
			}

			@Override
			public void prepareNewScreen() {

			}

			@Override
			public String gotoScreen() {
				return GameCenter.SCREEN_PLAYING;
			}
		});
	}

	@Override
	public void returnScreen() {
		if (GameCenter.isMusic) {
			mMenuMusic.play();
		}
		
		mUICenter.setScreen(this);
		mUICenter.show();
	}

	@Override
	public void backScreen() {
		
	}
	
	@Override
	public void show() {
		super.show();
		if (GameCenter.isMusic) mMenuMusic.play();
	}
	
	@Override
	public void resume() {
		mUICenter.resume();
		if (GameCenter.isMusic) mMenuMusic.play();
		super.resume();
	}
	
	@Override
	public void pause() {
		mUICenter.pause();
		if (GameCenter.isMusic) mMenuMusic.pause();
		super.pause();
	}
	
	// ===========================================================
	// Methods
	// ===========================================================
	public void play() {
		transitionScreen(new ITranScreenAction() {
			
			@Override
			public void onLoadComplete() {
			}
			
			@Override
			public void onLoad() {
//				mMenuUI.unShowMenu();
			}
			
			@Override
			public boolean isNeedUpdateAsset() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public void prepareNewScreen() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public String gotoScreen() {
				// TODO Auto-generated method stub
				return GameCenter.SCREEN_PLAYING;
			}
		});
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
